package com.wanghua.mall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.wanghua.common.utils.HttpUtils;
import com.wanghua.common.utils.R;
import com.wanghua.mall.auth.constant.AuthConstant;
import com.wanghua.mall.auth.openfeign.UserClient;
import com.wanghua.mall.auth.vo.GiteeAccessTokenVo;
import com.wanghua.common.vo.UserResponseVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wanghua
 * @create 2021-09-06 18:27
 * 处理第三方登录功能
 */
@Controller
@RequestMapping("/auth2.0")
public class Auth2Controller {
    @Resource
    private UserClient userClient;

    //Gitee登录
    @GetMapping("/gitee")
    public String gitee(@RequestParam("code") String code, HttpSession httpSession) throws Exception {
        Map<String, String> query = new HashMap<>();
        Map<String, String> headers = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        query.put("grant_type", "authorization_code");
        query.put("code", code);
        query.put("client_id", AuthConstant.CLIENT_ID);
        query.put("redirect_uri", AuthConstant.REDIRECT_URI);
        query.put("client_secret", AuthConstant.CLIENT_SECRET);
        //1、获取access_token
        HttpResponse httpResponse = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", headers, query, bodys);
        //2、处理
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            //成功
            String accessTokenVoJson = EntityUtils.toString(httpResponse.getEntity());
            GiteeAccessTokenVo giteeAccessTokenVo = JSON.parseObject(accessTokenVoJson, GiteeAccessTokenVo.class);
            String access_token = giteeAccessTokenVo.getAccess_token();
            //3、通过access_token获取用户详细信息 //https://gitee|.com/api/v5/user?access_token=5ec09937599ad3c2751d9d2dc4964eba
            HashMap<String, String> map = new HashMap<>();
            map.put("access_token", access_token);
            HttpResponse response = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", headers, map);
            if (response.getStatusLine().getStatusCode() == 200) {
                String giteeUserVoJson = EntityUtils.toString(response.getEntity());
                //远程调用
                R r = userClient.giteeLogin(giteeUserVoJson);
                if (r.getCode() == 0) {
                    UserResponseVo loginUser = JSON.parseObject((String) r.get("data"), UserResponseVo.class);
                    httpSession.setAttribute("loginUser", loginUser);
                    return "redirect:http://mall.com";
                }

            }
        }
        return "redirect:http://auth.mall.com/login.html";
    }
}
